//
//  RegisterAndLoginViewController.m
//  Router
//
//  Created by Zhen Huang on 2018/6/19.
//  Copyright © 2018年 Zhen Huang. All rights reserved.
//

#import "RegisterAndLoginViewController.h"
#import "HRegisterViewController.h"
#import "HResetPassWordViewController.h"
#import "HHomeViewController.h"
#import "AppDelegate.h"
@interface RegisterAndLoginViewController ()
<UITextFieldDelegate>
@property (nonatomic, strong)UIImageView *image;
@property (nonatomic, strong)UILabel *groupLabel;
@property (nonatomic, strong)UITextField *userNameText;
@property (nonatomic, strong)UITextField *passWordText;
@property (nonatomic, strong)UIImageView *backGround;
@end

@implementation RegisterAndLoginViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBackGroundColor;
    [[NSNotificationCenter defaultCenter] addObserver:self
                                             selector:@selector(UITextFieldTextDidChangeEditingNotification:)
                                                 name:UITextFieldTextDidChangeNotification
                                               object:nil];
    [self.view addSubview:self.backGround];
    [self.backGround addSubview:self.image];
    [self createUserCodeAndPassWord];
}
- (void)UITextFieldTextDidChangeEditingNotification:(NSNotification *)noti {
    UITextField *textField = noti.object;
    if (textField.tag == 101) {
        self.userNameText.text = textField.text;
    }else if (textField.tag == 102) {
        self.passWordText.text = textField.text;
    }
}
- (UIImageView *)image {
    if (!_image) {
        _image = [[UIImageView alloc]initWithFrame:CGRectMake(30, NavBarHeight + 35, 90, 90)];
        if (iPhone5) {
            _image.width = 70;
            _image.height = 70;
        }
        _image.image = [UIImage imageNamed:@"logo"];
        _image.contentMode = UIViewContentModeScaleAspectFit;
        _image.centerX = self.view.centerX;
        _image.layer.masksToBounds = YES;
        _image.layer.cornerRadius = 10;
    }
    return _image;
}
- (UIImageView *)backGround {
    if (!_backGround) {
        _backGround = [[UIImageView alloc]initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight)];
        _backGround.image = [UIImage imageNamed:@"BackGround"];
        _backGround.userInteractionEnabled = YES;
    }
    return _backGround;
}
- (void)createUserCodeAndPassWord {
    NSArray *imageArray = @[@"用户名",@"密码"];
    for (int i = 0; i < 2; i ++) {
        UIView *lineView = [[UIView alloc]initWithFrame:CGRectMake(20, self.image.bottom + 90 + 56 *i, ScreenWidth - 40, 1)];
        lineView.backgroundColor = [Networking colorWithHexString:@"#455A6A"];
        UIImageView *image = [[UIImageView alloc]initWithFrame:CGRectMake(25, self.image.bottom + 90 + 70 * i, 23, 30)];
        image.image = [UIImage imageNamed:imageArray[i]];
        image.top = lineView.bottom - 42;
        image.contentMode = UIViewContentModeScaleAspectFit;
        [self.backGround addSubview:lineView];
        [self.backGround addSubview:image];
    }
    //手机号
    self.userNameText = [[UITextField alloc]initWithFrame:CGRectMake(60, 53 + self.image.bottom,150, 21)];
    self.userNameText.placeholder = @"用户名/手机号";
    self.userNameText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.userNameText.placeholder attributes:@{NSForegroundColorAttributeName: [Networking colorWithHexString:@"#455A6A"]}];
    self.userNameText.delegate = self;
    self.userNameText.font = [UIFont systemFontOfSize:18];
    self.userNameText.textColor = kTitleBackGroundColor;
    [self.backGround addSubview:self.userNameText];
    self.userNameText.tag = 101;
    //密码
    self.passWordText = [[UITextField alloc]initWithFrame:CGRectMake(60, 112 + self.image.bottom,150,21)];
    self.passWordText.placeholder = @"密码";
    self.passWordText.delegate = self;
    self.passWordText.font = [UIFont systemFontOfSize:18];
    self.passWordText.attributedPlaceholder = [[NSAttributedString alloc] initWithString:self.passWordText.placeholder attributes:@{NSForegroundColorAttributeName: [Networking colorWithHexString:@"#455A6A"]}];
    self.passWordText.textColor = kTitleBackGroundColor;
    self.passWordText.tag = 102;
    [self.backGround addSubview:self.passWordText];
    //忘记密码
    UIButton *forgetPassWordButton = [[UIButton alloc]initWithFrame:CGRectMake(ScreenWidth - 95,self.passWordText.bottom + 35,75,21)];
    forgetPassWordButton.titleLabel.font = [UIFont fontWithName:PingFangSCRegular size:15];
    forgetPassWordButton.contentHorizontalAlignment = UIControlContentHorizontalAlignmentRight;
    [forgetPassWordButton setTitleColor:[Networking colorWithHexString:@"#409EFF"] forState:UIControlStateNormal];
    [forgetPassWordButton setTitle:@"忘记密码？" forState:UIControlStateNormal];
    [[forgetPassWordButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        HResetPassWordViewController *HResetPassWordVC = [[HResetPassWordViewController alloc]init];
        HResetPassWordVC.title = @"重置密码";
        [self.navigationController pushViewController:HResetPassWordVC animated:YES];
    }];
    [self.backGround addSubview:forgetPassWordButton];
    
    //登录按钮
    UIButton *loginButton = [[UIButton alloc]initWithFrame:CGRectMake(28,ScreenHeight - 150,ScreenWidth - 56,55)];
    loginButton.layer.masksToBounds = YES;
    loginButton.layer.cornerRadius = 25;
    if (iPhone5) {
        loginButton.height = 40;
        loginButton.layer.cornerRadius = 20;
    }
    loginButton.titleLabel.font = [UIFont fontWithName:PingFangSCRegular size:16];
    loginButton.backgroundColor = [Networking colorWithHexString:@"#409EFF"];
    [loginButton setTitleColor:[UIColor whiteColor] forState:UIControlStateNormal];
    [loginButton setTitle:@"立即登录" forState:UIControlStateNormal];
    [[loginButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        NSString *url = F(@"%@/tokens", BaseUrl);
        NSDictionary *paramDic = @{@"username":self.userNameText.text?:@"",
                                   @"password":self.passWordText.text?:@""
                                   };
        NSString *param = [self dictionaryToJson:paramDic];
        [self receivePOSTWithTokenURLString:url method:@"POST" body:param Token:nil Blocks:^(NSDictionary *tempData, NSInteger state) {
            if (tempData) {
                [[TMCache sharedCache]setObject:[tempData objectForKey:@"token"] forKey:@"Token"];
                [[TMCache sharedCache]setObject:[[tempData objectForKey:@"user"]objectForKey:@"username"] forKey:@"UserName"];
                AppDelegate *app = (AppDelegate *)[[UIApplication  sharedApplication] delegate];
                [app rootViewToTabBar];
            }
        }];
    }];
    [self.backGround addSubview:loginButton];
    //注册按钮
    UIButton *registerButton = [[UIButton alloc]initWithFrame:CGRectMake(28,loginButton.bottom + 10,ScreenWidth - 56,55)];
    registerButton.layer.masksToBounds = YES;
    registerButton.layer.cornerRadius = 25;
    if (iPhone5) {
        registerButton.height = 40;
        registerButton.layer.cornerRadius = 20;
    }
    registerButton.titleLabel.font = [UIFont fontWithName:PingFangSCRegular size:16];
    [registerButton setTitleColor:[Networking colorWithHexString:@"#409EFF"] forState:UIControlStateNormal];
    [registerButton setTitle:@"立即注册" forState:UIControlStateNormal];
    registerButton.layer.borderColor = [[Networking colorWithHexString:@"#409EFF"] CGColor];
    registerButton.layer.borderWidth = 1.0f;
    registerButton.layer.masksToBounds = YES;
    [[registerButton rac_signalForControlEvents:UIControlEventTouchUpInside]subscribeNext:^(id x) {
        HRegisterViewController *HRegisterVC = [[HRegisterViewController alloc]init];
        HRegisterVC.title = @"注册";
        [self.navigationController pushViewController:HRegisterVC animated:YES];
    }];
    [self.backGround addSubview:registerButton];
}
//点击界面的任意地方，回收键盘
-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
    [self.view endEditing:YES];
}
//回收键盘
- (BOOL)textFieldShouldReturn:(UITextField *)textField {
    [textField resignFirstResponder];
    return YES;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}



@end
